package com.xiaocz.hblsubstitutedriving.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.xiaocz.common.Common;
import com.xiaocz.common.http.AsyncHttpPostFormData;
import com.xiaocz.common.http.AsyncHttpResponse;
import com.xiaocz.common.http.AsyncOkHttpClient;
import com.xiaocz.common.http.AsyncResponseHandler;
import com.xiaocz.common.model.RspModel;
import com.xiaocz.common.utils.AccountManagers;
import com.xiaocz.common.widgets.load.LoadFactory;
import com.xiaocz.common.widgets.load.core.LoadManager;
import com.xiaocz.common.widgets.load.view.AbsView;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.base.BaseActivity;
import com.xiaocz.hblsubstitutedriving.model.WithDrawDeposit;
import com.xiaocz.hblsubstitutedriving.widget.EmptyView;
import com.xiaocz.hblsubstitutedriving.widget.ErrorView;
import com.xiaocz.hblsubstitutedriving.widget.LoadingView;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;

/**
 * Time:2019/7/23
 * Created by xiaocz
 * Describe 提现详情
 */
public class WithdrawDepositInfoActivity extends BaseActivity {

    @BindView(R.id.layout_base)
    LinearLayout layoutBase;
    @BindView(R.id.tv_time_name)
    TextView tvTimeName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_opening_bank)
    TextView tvOpeningBank;
    @BindView(R.id.tv_card_no)
    TextView tvCardNo;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_sxf)
    TextView tvSxf;
    @BindView(R.id.tv_state)
    TextView tvState;


    private void showWithDrawDepositInfo(WithDrawDeposit drawDeposit) {
        tvTime.setText(drawDeposit.getExaminetime());
        tvOpeningBank.setText(drawDeposit.getBankname());
        tvCardNo.setText(drawDeposit.getCardNo());
        tvMoney.setText(String.valueOf(drawDeposit.getMoney()));
        tvSxf.setText(drawDeposit.getServiceCharge());
        switch (drawDeposit.getStatus()) {
            case 1://1-提现中 2-提现成功 3-提现失败
                tvTime.setText(drawDeposit.getCreatetime());
                tvTimeName.setText("申请时间");
                tvState.setText("提现中");
                break;
            case 2:
                tvState.setText("提现成功");
                break;
            case 3:
                tvState.setText(String.valueOf("提现失败" + drawDeposit.getRemark()));
                break;
        }
    }



    /**
     * DATA
     */
    private LoadManager mLoadManager;
    private String id;

    public static void show(Context context, String id) {
        Intent intent = new Intent(context, WithdrawDepositInfoActivity.class);
        intent.putExtra("ID", id);
        context.startActivity(intent);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        if (bundle != null) {
            id = bundle.getString("ID");
        }
        return !TextUtils.isEmpty(id);
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
        mLoadManager = LoadFactory.getInstance().register(layoutBase, new AbsView.OnReloadListener() {
            @Override
            public void onReload(View view) {
                mLoadManager.showStateView(LoadingView.class);
                webWithdrawDepositInfo();
            }
        });
        webWithdrawDepositInfo();
    }

    /**
     * 提现详情
     */
    private void webWithdrawDepositInfo() {
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("driver_id", AccountManagers.getUserId());
        asyncHttpPostFormData.addFormData("orderNo", id);
        okHttpClient.post(Common.Constant.API_IP + "api/TiXian/QueryEntityDriverTiXianOrder", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                mLoadManager.showStateView(ErrorView.class);
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                Type type = new TypeToken<RspModel<WithDrawDeposit>>() {
                }.getType();
                RspModel<WithDrawDeposit> rspModel = getAnalysisBase(mLoadManager, response, type, "我的请求");
                if (rspModel == null) {
                    mLoadManager.showStateView(EmptyView.class);
                    return;
                }
                switch (rspModel.getCode()) {
                    case 200:
                        if (rspModel.getData() != null && !"[]".equals(rspModel.getData().toString())) {
                            showWithDrawDepositInfo(rspModel.getData());
                        } else {
                            App.getInstance().showMessage(rspModel.getMessage());
                        }
                        break;
                    default:
                        mLoadManager.showStateView(ErrorView.class);
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }


    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_withdraw_deposit_info;
    }
}
